package com.ifcar.app.util;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ifcar.app.activity.R;

public class myPreference extends DialogPreference {

	private ListView offlineListView;

	private MergeAdapter adapter;
	private LayoutInflater mInflater;
	private RelativeLayout rbLayoutWifi;
	private RelativeLayout rbLayout;
	private RelativeLayout rbLayoutClose;
	
	private RadioButton rbWifi;
	private RadioButton rbClose;
	private RadioButton rb;
	
	private String mValue = "";
	private Context mContext;

	private boolean initStatus = false;

	public myPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		setDialogLayoutResource(R.layout.offline_read_layout);
		mContext = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	protected void onBindDialogView(View view) {
		// TODO Auto-generated method stub
		super.onBindDialogView(view);
		adapter = new MergeAdapter();
		offlineListView = (ListView) view.findViewById(R.id.offline_read_list);

		rbLayout = (RelativeLayout) mInflater.inflate(R.layout.open_offline_rb,
				null);
		rbLayoutWifi = (RelativeLayout) mInflater.inflate(
				R.layout.open_offline_rb, null);
		rbLayoutClose = (RelativeLayout) mInflater.inflate(
				R.layout.open_offline_rb, null);
		
		rbLayoutClose.setClickable(true);
		rbLayout.setClickable(true);
		rbLayoutWifi.setClickable(true);
		rbLayoutClose.setOnClickListener(clickListener);
		rbLayout.setOnClickListener(clickListener);
		rbLayoutWifi.setOnClickListener(clickListener);
		
		rbClose = (RadioButton) rbLayoutClose.findViewById(R.id.open_offline_radio);
		rbWifi = (RadioButton) rbLayoutWifi.findViewById(R.id.open_offline_radio);
		rb = (RadioButton) rbLayout.findViewById(R.id.open_offline_radio);
		
		String value = "";
		if(initStatus) {
			value = mValue;
			initStatus = false;
		} else {
			value = getPersistedString(mContext.getResources().getString(R.string.wifi_open));
		}
		
        if(value.equals(mContext.getResources().getString(R.string.wifi_open))) {
        	rbClose.setChecked(false);
        	rbWifi.setChecked(true);
        	rb.setChecked(false);
        } else if(value.equals(mContext.getResources().getString(R.string.always_open))) {
        	rbClose.setChecked(false);
        	rbWifi.setChecked(false);
        	rb.setChecked(true);
        } else {
        	rbClose.setChecked(true);
        	rbWifi.setChecked(false);
        	rb.setChecked(false);
        }
		
		rbClose.setOnCheckedChangeListener(radioCheckedListener);
		rb.setOnCheckedChangeListener(radioCheckedListener);
		rbWifi.setOnCheckedChangeListener(radioCheckedListener);

		((TextView) rbLayoutClose.findViewById(R.id.open_offline_rb_text)).setText("关闭");
		((TextView) rbLayout.findViewById(R.id.open_offline_rb_text))
				.setText(mContext.getResources().getString(R.string.always_open));
		((TextView) rbLayoutWifi.findViewById(R.id.open_offline_rb_text))
				.setText(mContext.getResources().getString(R.string.wifi_open));
		
		adapter.addView(rbLayoutWifi);		
		adapter.addView(mInflater.inflate(
				R.layout.footerdivider, null));
		adapter.addView(rbLayout);
		adapter.addView(mInflater.inflate(
				R.layout.footerdivider, null));		
		adapter.addView(rbLayoutClose);
		adapter.addView(mInflater.inflate(
				R.layout.footerdivider, null));

		offlineListView.setAdapter(adapter);
	}

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		super.onDialogClosed(positiveResult);
		if (positiveResult) {	
			if(rb.isChecked()) {
				setValue(mContext.getResources().getString(R.string.always_open));
			} else if(rbWifi.isChecked()) {
				setValue(mContext.getResources().getString(R.string.wifi_open));
			} else {
				setValue(mContext.getResources().getString(R.string.shut_down));
			}
		} 

	}
	
	public void setValue(String value) {
		final boolean wasBlocking = shouldDisableDependents();
		
		mValue = value;
		persistString(value);
		
		final boolean isBlocking = shouldDisableDependents(); 
	    if (isBlocking != wasBlocking) {
	        notifyDependencyChange(isBlocking);
	    }
	}
	
	@Override
	protected Object onGetDefaultValue(TypedArray a, int index) {
		return a.getString(index);
	}

	@Override
	protected void onSetInitialValue(boolean restorePersistedValue,	Object defaultValue) {
		String value;
		initStatus = true;
		if(restorePersistedValue) {
			value = getPersistedString(mContext.getResources().getString(R.string.wifi_open));
		}
		else {
			value = defaultValue.toString();
		}
        setValue(value);
       
	}
	
	private RadioButton.OnCheckedChangeListener radioCheckedListener = new RadioButton.OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			if(isChecked) {
				if(buttonView == rb) {
					rbWifi.setChecked(false);
		        	rbClose.setChecked(false);
				} else if(buttonView == rbWifi){
					rb.setChecked(false);
					rbClose.setChecked(false);		        	
				} else {
					rb.setChecked(false);
					rbWifi.setChecked(false);
				}
			}

		}
		
	};
	
	private View.OnClickListener clickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			View view = ((RelativeLayout)v).getChildAt(1);
			if(view instanceof CheckBox) {
				if(((CheckBox) view).isChecked()) {
					((CheckBox) view).setChecked(false);
				} else {
					((CheckBox) view).setChecked(true);
				}
			} else {
				if(((RadioButton) view).isChecked()) {
					//((RadioButton) view).setChecked(false);
				} else {
					((RadioButton) view).setChecked(true);
				}
			}
		}
	};

}
